package com.hourse.app.service.impl;

import com.hourse.app.entity.ExternalAccountEntity;
import com.hourse.app.service.ExternalAccountService;
import com.hourse.app.service.PlatformOyxService;
import com.hourse.app.service.TickerService;
import com.hourse.app.vo.rms.BingExternalAccountVo;
import com.hourse.app.vo.web.PlatformSession;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.exception.ValidateException;
import com.hourse.platform.service.impl.PublicServiceImpl;
import com.hourse.platform.utils.AppTools;
import com.hourse.platform.utils.SqlParams;
import com.hourse.platform.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者：lichenguang
 * @描述：【外部账户信息（三方账户）】
 * @创建：2022/2/15 11:25
 */
@Service
@Slf4j
public class ExternalAccountServiceImpl extends PublicServiceImpl implements ExternalAccountService {

  @Autowired
  private TickerService tickerService;

  @Autowired
  private PlatformOyxService platformOyxService;

  /**
   * @描述：【绑定外部账号信息】
   * @作者：lichenguang
   * @时间: 2022/2/15 11:54
   */
  @Override
  @Transactional
  public void binding(BingExternalAccountVo vo) throws AppException {
    try {
      ExternalAccountEntity entity = (ExternalAccountEntity) vo.bindEnity(ExternalAccountEntity.class);
      entity.setId(vo.getId());
      if (StringTools.isNull(entity.getId())) {
        entity.setCreateTime(new Date());
        entity.setCreateUser(AppTools.GetCurrentUserId());
        entity.setIsAvailable("1");
      } else {
        SqlParams sqlParams = new SqlParams();
        sqlParams.append("update ExternalAccountEntity a set a.platform =:platform");
        sqlParams.append(",a.uKey =:uKey");
        sqlParams.append(",a.uSecret =:uSecret");
        sqlParams.append(",a.passphrase =:passphrase");
        sqlParams.append(" where a.id = :id");

        sqlParams.add("platform", entity.getPlatform());
        sqlParams.add("uKey", entity.getUKey());
        sqlParams.add("uSecret", entity.getUSecret());
        sqlParams.add("id", entity.getId());
        sqlParams.add("passphrase", entity.getPassphrase());

        baseDao.executeUpdate(sqlParams.getSql(), sqlParams.getParams());

      }
      baseDao.saveOrUpdate(entity);
      vo.setId(entity.getId());

    } catch (ValidateException e) {
      e.printStackTrace();
      throw new AppException(e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      throw new AppException("绑定外部账号信息");
    }
  }

  /**
   * @描述：【查询绑定三方账号地址】
   * @作者：lichenguang
   * @时间: 2022/2/15 13:28
   */
  @Override
  @Transactional(readOnly = true)
  public Object list(String userId) {
    SqlParams sqlParams = SqlParams.New();
    sqlParams.append("from ExternalAccountEntity a ");
    if (StringTools.isNotNull(userId)) sqlParams.append(" where a.userId = :userId");
    sqlParams.append(" and isAvailable = '1'");
    sqlParams.append(" order by a.createTime desc");
    sqlParams.add("userId", userId);
    return baseDao.getItemListByNativeHQL(sqlParams.getSql(), sqlParams.getParams());
  }

  /**
   * @描述：【三方账号启用禁用】
   * @作者：lichenguang
   * @时间: 2022/2/15 13:41
   */
  @Override
  @Transactional(rollbackFor = AppException.class)
  public void disabled(String id, String isUse) throws Exception {
    SqlParams sqlParams = new SqlParams();
    sqlParams.append("update ExternalAccountEntity a set a.isUse =:isUse where a.id = :id");
    sqlParams.add("isUse", isUse);
    sqlParams.add("id", id);

    baseDao.executeUpdate(sqlParams.getSql(), sqlParams.getParams());
  }

  /**
   * @描述：【删除绑定三方账号信息】
   * @作者：lichenguang
   * @时间: 2022/2/15 13:50
   */
  @Override
  @Transactional(rollbackFor = AppException.class)
  public void delete(String id) throws Exception {
    SqlParams sqlParams = new SqlParams();
    sqlParams.append("update ExternalAccountEntity a set a.isAvailable =:isAvailable where a.id = :id");
    sqlParams.add("isAvailable", "0");
    sqlParams.add("id", id);

    baseDao.executeUpdate(sqlParams.getSql(), sqlParams.getParams());
  }

  /**
   * @描述：【查询对应三方开启的账号信息】
   * @作者：lichenguang
   * @时间: 2022/2/25 0:07
   */
  @Override
  @Transactional(readOnly = true)
  public List<String> isUseByUserId(String userId) {
    SqlParams sqlParams = SqlParams.New();
    sqlParams.append("select a.platform");
    sqlParams.append(" from ExternalAccountEntity a");
    sqlParams.append(" where isUse = '1'");
    sqlParams.append(" where isAvailable = '1'");
    sqlParams.append(" and userId = :userId");
    sqlParams.add("userId", userId);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(), sqlParams.getParams());
  }

  @Override
  @Transactional(readOnly = true)
  public ExternalAccountEntity getIsUsePlatform(String userId) {
    SqlParams sqlParams = SqlParams.New();
    sqlParams.append("from ExternalAccountEntity a");
    sqlParams.append(" where isUse = '1'");
    sqlParams.append(" and isAvailable = '1'");
    sqlParams.append(" and userId = :userId");
    sqlParams.add("userId", userId);
    return (ExternalAccountEntity) baseDao.execSingleQuery(sqlParams.getSql(), sqlParams.getParams());

  }

  @Override
  public boolean connectTest(String platformId) throws AppException {

    ExternalAccountEntity entity = (ExternalAccountEntity) findById(platformId, ExternalAccountEntity.class);
    String uKey = entity.getUKey();
    String uSecret = entity.getUSecret();
    String platform = entity.getPlatform();

    //platformOyxService.balance(uKey, uSecret);

    return false;
  }
}
